package com.ttbj.controller.dispatch;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ttbj.api.dispatch.DispatchService;
import com.ttbj.api.dto.dispatch.DispatchDto;
import com.ttbj.api.exception.BusinessException;
import com.ttbj.constant.annotation.SystemControllerLog;
import com.ttbj.util.ViewDataUtil;

@Controller
public class DispatchController {
	
	@Autowired
	private DispatchService dispatchService;
	
	
	/**
	 * 自动/手动派单配置参数
	 * @return
	 */
	@RequestMapping(value="/dis/dispatch_info.htm")
	public String dispatch_info() {
		return "dispatch/dispatch_info";
	}
	
	
	/**
	 * 查询手动/自动派单的参数配置
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/dis/getDispatchInfo.htm", method=RequestMethod.GET, produces={"text/html;charset=UTF-8;","application/json;"})
	@ResponseBody
	public String getDispatchInfo(HttpServletRequest request) {
		try {
			DispatchDto dispatchDto = this.dispatchService.findDispatchInfo();
			return ViewDataUtil.buildSuccessJson("自动/手动派单配置信息", dispatchDto);
		} catch (BusinessException e) {
			return ViewDataUtil.buildFailureJson(e.getErrorCode(), e.getMessage());
		}
	}
	
	/**
	 * 更改手动/自动派单，以及自动派单时需要用到的cron表达式
	 * @param request
	 * @param dispatchDto 参数配置信息
	 * @return
	 */
	@RequestMapping(value="/dis/getDispatchInfo.htm", method=RequestMethod.POST, produces={"text/html;charset=UTF-8;","application/json;"})
	@ResponseBody
	@SystemControllerLog(description="修改手动/自动派单配置")
	public String modifyDispatchInfo(DispatchDto dispatchDto) {
		try {
			this.dispatchService.editDispatchInfo(dispatchDto);
			return ViewDataUtil.buildSuccessJson("更改成功");
		} catch (BusinessException e) {
			return ViewDataUtil.buildFailureJson(e.getErrorCode(), e.getMessage());
		}
	}
}
